rm(list = ls())

# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"



setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)


# load press releases by party by month
weather <- read_csv(paste0(taskdir, 'input/press_releases.csv'))
weather


# load press releases by event type by month
weather2 <- read_csv(paste0(taskdir, 'input/press_releases_byeventype.csv'))
weather2


# First Plot 
# Main plot with percentages as another axis
p4<- ggplot(weather%>%filter(party %in% c('D','R'))%>%
					   mutate(party = ifelse(party=="D", "Democrats", "Republicans")))+
	geom_col(aes(x = date, y = total_pr, fill = 'Extreme Weather Only'), 
			color = 'grey80')+
	geom_col(aes(x = date, y = climate, fill = 'Extreme Weather and Climate Change'), 
			color = 'grey20')+
	scale_fill_manual(name = "Press Releases Mention",
					   values = c("grey20", "grey80"))+
	facet_wrap(~party, nrow=2)+
	theme_bw()+
	scale_x_date(date_breaks = "1 year",date_labels="%Y")+
	theme(legend.position = 'bottom',
		  legend.direction = "vertical")+
	xlab('Year')+
	ylab('Number of Press Releases')+
	ggtitle("Monthly Number of Press Releases that Mention Extreme Weather and that Mention Climate Change (by Party)")
df2<- weather%>%filter(party %in% c('D','R'))%>%
					   mutate(party = ifelse(party=="D", 
					   		"Democrats", "Republicans"))%>%
					   group_by(year,party)%>%
					   summarize(total = sum(total_pr,na.rm = T),
					   			 climate = sum(climate,na.rm = T))%>%
					   mutate(pc_climate=climate/total)%>%
					   mutate(date = as.Date(paste0(year+1,'-01-01')) )


p5<- p4+geom_point(data = df2,aes(x = date , y = pc_climate*500), 
	size = 0.5, color = "green4")+
	geom_path(data = df2,aes(x = date , y = pc_climate*500), 
		size = 0.5, color = "green4")+
	scale_y_continuous(
	    sec.axis = sec_axis(~./500, name = "% Mentioning Climate Change", 
	      labels = function(b) { paste0(round(b * 100, 0), "%")})) + 
	  theme(
	      axis.title.y.right = element_text(color = "green4"),
	      axis.text.y.right = element_text(color = "green4"))+
	geom_vline(aes(xintercept = as.Date("2018-10-01"), color = 'IPCC SR1.5 Release Date'), alpha = 0.7)+
	scale_color_manual(name = "Vertical Line Denotes:", values = c('IPCC SR1.5 Release Date' = "red"))
p5
ggsave(paste0(taskdir, '/output/descriptive_plots/climatepr_ts1_wpc.pdf'),
	p5, width = 10, height = 5.04,
				dpi = 600)



# Second Plot 
# Press release by Type
p3<- ggplot(weather2%>%filter(!is.na(type)))+
	geom_col(aes(x = date, y = total_pr, fill = 'Extreme Weather Only'), 
			color = 'grey80')+
	geom_col(aes(x = date, y = climate, fill = 'Extreme Weather and Climate Change'), 
			color = 'grey20')+
	scale_fill_manual(name = "Press Releases Mention",
					   values = c("grey20", "grey80"))+
	facet_wrap(~type, ncol=1, scales = "free")+
	theme_bw()+
	scale_x_date(date_breaks = "1 year",date_labels="%Y")+
	theme(legend.position = 'bottom',
		  legend.direction = "vertical")+
	xlab('Year')+
	ylab('Number of Press Releases')
p3
ggsave(paste0(taskdir, '/output/descriptive_plots/climatepr_ts_byevent.pdf'),
	p3, width = 6, height = 7,
				dpi = 600)


















